package client;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import pt.iscte.dcti.pa.controller.WorksheetController;
import pt.iscte.dcti.pa.model.WorksheetModel;

public class CallbackHandler extends UnicastRemoteObject implements
		ICallbackHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6164046399752149941L;
	pt.iscte.dcti.pa.controller.WorksheetController controller;

	public CallbackHandler(final WorksheetController _controller) throws RemoteException {
		super();
		this.controller = _controller;
	}

	@Override
	public String notifyMe(String message) throws RemoteException {
		String returnMessage = String.format("Callback recebido (y): %s", message);
		System.out.println(returnMessage);
		return returnMessage;
	}

	@Override
	public String updateCellValue(int abscissa, int ordinate, String value)
			throws RemoteException {
		
		try {
			System.out.println(String.format("Received notification to update the cell [%s,%s] value: %s", abscissa, ordinate, value));
			//TODO debug
			this.controller.updateColabValue(abscissa, ordinate, value);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return value;
	}

	@Override
	public boolean lockCell(int abscissa, int ordinate) throws RemoteException {
		try {
			this.controller.lockCell(abscissa, ordinate);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		
	}

}
